#!/bin/bash

# 跨平台构建脚本
# 支持Android和iOS的自动化构建

set -e

echo "🚀 开始跨平台构建..."

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_message() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

print_step() {
    echo -e "${BLUE}[STEP]${NC} $1"
}

# 检查Flutter环境
check_flutter() {
    print_step "检查Flutter环境..."
    if ! command -v flutter &> /dev/null; then
        print_error "Flutter未安装或未添加到PATH"
        exit 1
    fi
    
    flutter doctor
    print_message "Flutter环境检查完成"
}

# 清理构建缓存
clean_build() {
    print_step "清理构建缓存..."
    flutter clean
    flutter pub get
    print_message "构建缓存清理完成"
}

# 构建Android APK
build_android_apk() {
    print_step "构建Android APK..."
    
    # 检查Android环境
    if ! flutter doctor | grep -q "Android toolchain"; then
        print_warning "Android工具链未配置，跳过Android构建"
        return
    fi
    
    # 构建调试版本
    flutter build apk --debug
    print_message "Android调试APK构建完成"
    
    # 构建发布版本
    flutter build apk --release
    print_message "Android发布APK构建完成"
    
    # 构建AAB (Google Play)
    flutter build appbundle --release
    print_message "Android App Bundle构建完成"
}

# 构建Android (原生)
build_android_native() {
    print_step "构建原生Android应用..."
    
    if [ -f "app/build.gradle.kts" ]; then
        # 使用Gradle构建
        ./gradlew clean
        ./gradlew assembleDebug
        ./gradlew assembleRelease
        ./gradlew bundleRelease
        print_message "原生Android构建完成"
    else
        print_warning "未找到原生Android项目文件"
    fi
}

# 构建iOS
build_ios() {
    print_step "构建iOS应用..."
    
    # 检查iOS环境
    if ! flutter doctor | grep -q "iOS toolchain"; then
        print_warning "iOS工具链未配置，跳过iOS构建"
        return
    fi
    
    # 检查是否在macOS上
    if [[ "$OSTYPE" != "darwin"* ]]; then
        print_warning "iOS构建需要在macOS上进行"
        return
    fi
    
    # 构建iOS应用
    flutter build ios --release --no-codesign
    print_message "iOS应用构建完成"
}

# 运行测试
run_tests() {
    print_step "运行测试..."
    flutter test
    print_message "测试完成"
}

# 分析代码
analyze_code() {
    print_step "分析代码..."
    flutter analyze
    print_message "代码分析完成"
}

# 显示构建结果
show_build_results() {
    print_step "构建结果:"
    
    # Android APK
    if [ -f "build/app/outputs/flutter-apk/app-release.apk" ]; then
        print_message "✅ Android APK: build/app/outputs/flutter-apk/app-release.apk"
    fi
    
    # Android AAB
    if [ -f "build/app/outputs/bundle/release/app-release.aab" ]; then
        print_message "✅ Android AAB: build/app/outputs/bundle/release/app-release.aab"
    fi
    
    # 原生Android APK
    if [ -f "app/build/outputs/apk/release/app-release.apk" ]; then
        print_message "✅ 原生Android APK: app/build/outputs/apk/release/app-release.apk"
    fi
    
    # iOS
    if [ -d "build/ios/archive/Runner.xcarchive" ]; then
        print_message "✅ iOS Archive: build/ios/archive/Runner.xcarchive"
    fi
}

# 主函数
main() {
    print_message "开始跨平台构建流程..."
    
    # 检查Flutter环境
    check_flutter
    
    # 清理构建缓存
    clean_build
    
    # 分析代码
    analyze_code
    
    # 运行测试
    run_tests
    
    # 构建Flutter应用
    build_android_apk
    build_ios
    
    # 构建原生Android应用
    build_android_native
    
    # 显示构建结果
    show_build_results
    
    print_message "🎉 跨平台构建完成！"
}

# 处理命令行参数
case "${1:-all}" in
    "android")
        check_flutter
        clean_build
        build_android_apk
        build_android_native
        ;;
    "ios")
        check_flutter
        clean_build
        build_ios
        ;;
    "test")
        check_flutter
        run_tests
        ;;
    "analyze")
        check_flutter
        analyze_code
        ;;
    "clean")
        clean_build
        ;;
    "all"|*)
        main
        ;;
esac 